package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.qc.CommonDefect;
import com.qidianit.mes.pro.mapper.CommonDefectMapper;
import com.qidianit.mes.pro.service.CommonDefectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 常见缺陷业务实现类
 * @Author: wang fei long
 * @Date: 2025/9/22 0022 - 14:49
 */
@Service
public class CommonDefectServiceImpl implements CommonDefectService {
    @Autowired
    private CommonDefectMapper commonDefectMapper;
    @Override
    public Result<PageInfo<CommonDefect>> selectList(CommonDefect commonDefect, int pageNum, int pageSize) {
        // 校验分页参数
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1 || pageSize > 100) {
            pageSize = 10; // 限制最大每页条数
        }
        PageHelper.startPage(pageNum, pageSize);
        List<CommonDefect> defectList = commonDefectMapper.selectList(commonDefect);

        PageInfo<CommonDefect> pageInfo = new PageInfo<>(defectList);
        return Result.success("查询成功", pageInfo);
    }

    @Override
    public Result<CommonDefect> selectById(Long id) {
        return Result.success(commonDefectMapper.selectById(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result save(CommonDefect commonDefect) {
        if (commonDefect.getDefectDescription() == null || commonDefect.getDefectDescription().trim().isEmpty()) {
            return Result.error("缺陷描述不能为空");
        }
        if (commonDefect.getDetectionItemType() == null || commonDefect.getDetectionItemType().trim().isEmpty()) {
            return Result.error("检测项类型不能为空");
        }
        if (commonDefect.getDefectLevel() == null || commonDefect.getDefectLevel().trim().isEmpty()) {
            return Result.error("缺陷等级不能为空");
        }
        int rows = commonDefectMapper.save(commonDefect);
        if (rows > 0) {
            return Result.success("新增缺陷记录成功", commonDefect.getId());
        } else {
            return Result.error("新增缺陷记录失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateById(CommonDefect commonDefect) {
        int rows = commonDefectMapper.updateById(commonDefect);
        if (rows > 0) {
            return Result.success("更新缺陷记录成功");
        } else {
            return Result.notFound("未找到该缺陷记录或记录已被删除");
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteById(Long id) {
        // 参数校验
        if (id == null) {
            return Result.error("缺陷ID不能为空");
        }

        // 执行删除操作（更新时间由XML中的now()处理）
        int rows = commonDefectMapper.deleteById(id);
        if (rows > 0) {
            return Result.success("删除缺陷记录成功");
        } else {
            return Result.notFound("未找到该缺陷记录或记录已被删除");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result batchDelete(Long[] ids) {
        // 参数校验
        if (ids == null || ids.length == 0) {
            return Result.error("请选择需要删除的缺陷记录");
        }

        // 执行批量删除操作（更新时间由XML中的now()处理）
        int rows = commonDefectMapper.batchDelete(ids);
        if (rows > 0) {
            return Result.success("成功删除 " + rows + " 条缺陷记录");
        } else {
            return Result.error("删除失败，所选记录可能已被删除");
        }
    }

}
